package cn.com.xxlibrary.action;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import cn.com.xxlibrary.orm.BooksBean;
import cn.com.xxlibrary.orm.ManagerInfoBean;
import cn.com.xxlibrary.service.IBookService;
import cn.com.xxlibrary.service.ISystemService;
import cn.com.xxlibrary.util.UtilTools;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@ParentPackage("myStruts")
@Namespace(value = "/")
public class BookAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private ISystemService systemService;

	@Resource
	private IBookService bookService;

	private BooksBean book;
	private int id;
	private String op;

	private String wd;

	// 默认页码
	private int pageNo = 1;
	// 默认分页大小
	private int pageSize = 10;

	/**
	 * 添加资料
	 * 
	 * @return
	 */
	@Actions(value = {
			@Action(value = "saveBook", results = { @Result(name = SUCCESS, location = "/listBook", type = "redirect"), @Result(name = "add", location = "/addBook", type = "redirect") }),
			@Action(value = "addBook", results = { @Result(name = SUCCESS, location = "/books/booksDetail.jsp") }) })
	public String addBook() {
		String re = SUCCESS;
		if (UtilTools.isNotNullorEmpty(book)) {
			if (book.getBookId()>0){
				re = "add";
			}
			bookService.addBook(book);
			book = null;
		}
		return re;
	}

	@Action(value = "delBook")
	public void delBook() throws IOException {
		bookService.delBook(id);
		UtilTools.sendMsg("删除成功！");
	}

	/*
	 * 
	 * @Action(value = "updataBook", results = { @Result(name = SUCCESS,
	 * location = "/listBook", type = "redirect") }) public String updataBook()
	 * { }
	 */
	@Action(value = "queryBook", results = {
			@Result(name = "guest", location = "/Guest/bookInfo.jsp"),
			@Result(name = "info", location = "/books/booksInfo.jsp"),
			@Result(name = "detail", location = "/books/booksDetail.jsp") })
	public String queryBook() {
		book = bookService.queryBook(id);
//		Object obj = ActionContext.getContext().getSession().get("SESSION_MANAGER_LOGIN");
//		if (!UtilTools.isNotNullorEmpty(obj)||"Guest".equals((((ManagerInfoBean)obj)).getManLoginName())){
//			return "guest";
//		}
		return op;
	}

	@Action(value = "listBook", results = { @Result(name = SUCCESS, location = "/books/booksList.jsp") })
	public String listBook() {
		int start = (pageNo - 1) * pageSize;
		Map map = bookService.listBook(start, pageSize);

		ActionContext.getContext().getSession()
				.put("SESSION_BOOK_LIST", map.get("queryResult"));
		ActionContext.getContext().getSession()
				.put("SESSION_BOOK_PAGESIZE", pageSize);
		ActionContext.getContext().getSession()
				.put("SESSION_BOOK_PAGENO", pageNo);
		ActionContext.getContext().getSession()
				.put("SESSION_BOOK_COUNT", map.get("count"));

		return SUCCESS;
	}

	// =====================================================

	public BooksBean getBook() {
		return book;
	}

	public void setBook(BooksBean book) {
		this.book = book;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public IBookService getBookService() {
		return bookService;
	}

	public void setBookService(IBookService bookService) {
		this.bookService = bookService;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getWd() {
		return wd;
	}

	public void setWd(String wd) {
		this.wd = wd;
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

}
